/**
* @author maze | guavestudios.com
* @license MIT License see LICENSE.TXT
* @version 0.1
*/
package com.guavestudios.fcon.plugins {
	import flash.net.SharedObject;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import com.guavestudios.fcon.api.IControlableConsole;
	import com.guavestudios.fcon.ConsoleImpl;
	import com.guavestudios.fcon.events.ConsoleExecuteEvent;
	import com.guavestudios.fcon.events.ConsoleKeyboardEvent;

	public class HistoryManager implements IConsolePlugin{
		
		//{ STATIC
	
		//}
		
		//{ MEMBERS
		private var history:Array;
		private var index:int=0;
		private var maxHistory:int = 50;
		private var sharedHistory:SharedObject;
		
		private var console:IControlableConsole;
		
		//}
		
		//{ GETTER-SETTER
		/**
		* TODO Insert description from GET maxHistory here.
		*  
		*  @return int=100
		*/
		public function getMaxHistory():int {
			return this.maxHistory;
		}
		/**
		* TODO Insert description from SET maxHistory here.
		*  
		*  @param pmaxHistory[int=100]
		*  @return
		*/
		public function setMaxHistory(pmaxHistory:int):void {
			this.maxHistory = pmaxHistory;
		}
		
		//}
		public function HistoryManager() {
			
			history=[];
			init();	
		}
		private function init():void {
			
			
			
		}
		//{ PUBLIC FUNCTIONS
		public function addHistory(s:String):void
		{
			history.push(s);
			index=history.length;
			
			if (index>maxHistory)
			{
				history.shift();
				index--;
			}
		}
		public function getHistoryPrev():String
		{
			if (hasPrev())
			{
				return history[--index];
			}
			return null;
		}
		public function getHistoryNext():String
		{
			if (hasNext())
			{
				return history[++index];
			}
			return null;
		}
		public function hasNext():Boolean
		{
			return index<history.length-1;
		}
		public function hasPrev():Boolean
		{
			return index>0;
		}
		
		//functions for interface
				/**
		* Called when the plugin is finally loaded
		* @param	c
		*/
		public function initPlugin(c:IControlableConsole):void
		{
			console = c;
			
			if (c.getConfig("HistoryManager.SaveHistory") != null)
			{
				sharedHistory = SharedObject.getLocal("__fcon");
				var hi:Array = sharedHistory.data["cmdHistory"];
				if (hi!=null)
				{
					history = hi;
					index = history.length;
				}
			}
			c.addEventListener(ConsoleKeyboardEvent.INPUT_KEYUP,input_keyup);
			c.addEventListener(ConsoleExecuteEvent.EXECUTE_USER,console_executeok);
		}
		/**
		* Request the unload for the plugin
		*/
		public function unloadPlugin():void
		{
			console.removeEventListener(ConsoleKeyboardEvent.INPUT_KEYUP,input_keyup);
			console.removeEventListener(ConsoleExecuteEvent.EXECUTE_USER,console_executeok);
		}
		/**
		* Current state of the plugin see @see com.guavestudios.fcon.plugins.PluginState
		*/
		public function get State():int
		{
			return 0;
		}
		/**
		* Plugin name
		*/
		public function get Name():String
		{
			return "HistoryManager";
		}
		/**
		* Plugin version
		*/
		public function get Version():String
		{
			return "r1";
		}
		//}
		
		//{ OVERRIDEN METHODS
		
		//}
		
		//{ PRIVATE FUNCTIONS
		private function input_keyup(e:ConsoleKeyboardEvent):void
		{
			var ip:TextField;
			switch (e.getInnerEvent().keyCode)
			{
				case Keyboard.UP:
					if (hasPrev())
					{
						ip=ConsoleImpl(console).getInput();
						ip.text=getHistoryPrev();
						ip.setSelection(ip.text.length,ip.text.length);
					}
					break;
				case Keyboard.DOWN:
					if (hasNext())
					{
						ip=ConsoleImpl(console).getInput();
						ip.text=getHistoryNext();
						ip.setSelection(ip.text.length,ip.text.length);
					}
					break;
			}
		}
		private function console_executeok(e:ConsoleExecuteEvent):void
		{
			addHistory(e.getExecute().join(" "));
			
			if (sharedHistory != null)
			{
				sharedHistory.data["cmdHistory"] = history;
			}
			
			
		}
		//}

	}
	
}
